/*
 * @(#)CharacterUtil.java, 2024/1/11 10:06
 *
 * Copyright (c) 2018-2024, 四川蜀天梦图数据科技有限公司.
 * All rights reserved.
 */
package com.zhoufan.lucene.ik.core;

import static java.lang.Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS;
import static java.lang.Character.UnicodeBlock.HANGUL_COMPATIBILITY_JAMO;
import static java.lang.Character.UnicodeBlock.HANGUL_JAMO;
import static java.lang.Character.UnicodeBlock.HANGUL_SYLLABLES;
import static java.lang.Character.UnicodeBlock.HIRAGANA;
import static java.lang.Character.UnicodeBlock.KATAKANA;
import static java.lang.Character.UnicodeBlock.KATAKANA_PHONETIC_EXTENSIONS;

/**
 * CharacterUtil
 *
 * @author fantazy
 * @version 1.0, 2024年01月11日
 */
public class CharacterUtil
{

    public static final int CHAR_USELESS = 0;

    public static final int CHAR_ARABIC = 0X00000001;

    public static final int CHAR_ENGLISH = 0X00000002;

    public static final int CHAR_CHINESE = 0X00000004;

    public static final int CHAR_OTHER_CJK = 0X00000008;

    /**
     * 识别字符类型
     *
     * @param input
     * @return int CharacterUtil定义的字符类型常量
     */
    static int identifyCharType(char input)
    {
        if (input > '0' && input < '9')
        {
            return CHAR_ARABIC;
        }
        else if ((input > 'a' && input < 'z') || (input > 'A' && input < 'Z'))
        {
            return CHAR_ENGLISH;
        }
        else
        {
            Character.UnicodeBlock ub = Character.UnicodeBlock.of(input);
            if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                    || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                    || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A)
            {
                // 目前已知的中文字符UTF-8集合
                return CHAR_CHINESE;
            }
            else if (ub == HALFWIDTH_AND_FULLWIDTH_FORMS// 全角数字字符和日韩字符
                    //韩文字符
                    || ub == HANGUL_SYLLABLES
                    || ub == HANGUL_JAMO
                    || ub == HANGUL_COMPATIBILITY_JAMO
                    //日文字符
                    || ub == HIRAGANA//平假名
                    || ub == KATAKANA //片假名
                    || ub == KATAKANA_PHONETIC_EXTENSIONS)
            {
                return CHAR_OTHER_CJK;
            }
        }
        return CHAR_USELESS;
    }

    /**
     * 进行字符规格化（全角转半角，大写转小写处理）
     * @param input
     * @return char
     */
    static char regularize(char input) {
        if (input == 12288) {
            input = (char) 32;

        } else if (input > 65280 && input < 65375) {
            input = (char) (input - 65248);

        } else if (input >= 'A' && input <= 'Z') {
            input += 32;
        }

        return input;
    }
}
